﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using HslCommunication.BasicFramework;

namespace HslCommunication.WebSocket
{
	/// <summary>
	/// websocket 的会话客户端
	/// </summary>
	// Token: 0x02000026 RID: 38
	public class WebSocketSession
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		// Token: 0x06000272 RID: 626 RVA: 0x0000A017 File Offset: 0x00008217
		public WebSocketSession()
		{
			this.Topics = new List<string>();
			this.ActiveTime = DateTime.Now;
			this.OnlineTime = DateTime.Now;
		}

		/// <summary>
		/// 当前客户端的激活时间
		/// </summary>
		// Token: 0x1700008C RID: 140
		// (get) Token: 0x06000273 RID: 627 RVA: 0x0000A050 File Offset: 0x00008250
		// (set) Token: 0x06000274 RID: 628 RVA: 0x0000A058 File Offset: 0x00008258
		public DateTime ActiveTime { get; set; }

		/// <summary>
		/// 获取当前的客户端的上线时间
		/// </summary>
		// Token: 0x1700008D RID: 141
		// (get) Token: 0x06000275 RID: 629 RVA: 0x0000A061 File Offset: 0x00008261
		// (set) Token: 0x06000276 RID: 630 RVA: 0x0000A069 File Offset: 0x00008269
		public DateTime OnlineTime { get; private set; }

		/// <summary>
		/// 当前客户端绑定的套接字对象
		/// </summary>
		// Token: 0x1700008E RID: 142
		// (get) Token: 0x06000277 RID: 631 RVA: 0x0000A072 File Offset: 0x00008272
		// (set) Token: 0x06000278 RID: 632 RVA: 0x0000A07A File Offset: 0x0000827A
		internal Socket WsSocket { get; set; }

		/// <summary>
		/// 当前客户端订阅的所有的Topic信息
		/// </summary>
		// Token: 0x1700008F RID: 143
		// (get) Token: 0x06000279 RID: 633 RVA: 0x0000A083 File Offset: 0x00008283
		// (set) Token: 0x0600027A RID: 634 RVA: 0x0000A08B File Offset: 0x0000828B
		public List<string> Topics { get; set; }

		/// <summary>
		/// 远程的客户端的ip及端口信息
		/// </summary>
		// Token: 0x17000090 RID: 144
		// (get) Token: 0x0600027B RID: 635 RVA: 0x0000A094 File Offset: 0x00008294
		// (set) Token: 0x0600027C RID: 636 RVA: 0x0000A09C File Offset: 0x0000829C
		public IPEndPoint Remote { get; set; }

		/// <summary>
		/// 当前的会话是否是问答客户端，如果是问答客户端的话，数据的推送是无效的。
		/// </summary>
		// Token: 0x17000091 RID: 145
		// (get) Token: 0x0600027D RID: 637 RVA: 0x0000A0A5 File Offset: 0x000082A5
		// (set) Token: 0x0600027E RID: 638 RVA: 0x0000A0AD File Offset: 0x000082AD
		public bool IsQASession { get; set; }

		/// <summary>
		/// 客户端请求的url信息，可能携带一些参数信息
		/// </summary>
		// Token: 0x17000092 RID: 146
		// (get) Token: 0x0600027F RID: 639 RVA: 0x0000A0B6 File Offset: 0x000082B6
		// (set) Token: 0x06000280 RID: 640 RVA: 0x0000A0BE File Offset: 0x000082BE
		public string Url { get; set; }

		/// <summary>
		/// 检查当前的连接对象是否在
		/// </summary>
		/// <param name="topic">主题信息</param>
		/// <returns>是否包含的结果信息</returns>
		// Token: 0x06000281 RID: 641 RVA: 0x0000A0C8 File Offset: 0x000082C8
		public bool IsClientSubscribe(string topic)
		{
			bool result = false;
			object obj = this.objLock;
			lock (obj)
			{
				result = this.Topics.Contains(topic);
			}
			return result;
		}

		/// <summary>
		/// 动态增加一个订阅的信息
		/// </summary>
		/// <param name="topic">订阅的主题</param>
		// Token: 0x06000282 RID: 642 RVA: 0x0000A11C File Offset: 0x0000831C
		public void AddTopic(string topic)
		{
			object obj = this.objLock;
			lock (obj)
			{
				bool flag2 = !this.Topics.Contains(topic);
				if (flag2)
				{
					this.Topics.Add(topic);
				}
			}
		}

		/// <summary>
		/// 动态移除一个订阅的信息
		/// </summary>
		/// <param name="topic">订阅的主题</param>
		// Token: 0x06000283 RID: 643 RVA: 0x0000A180 File Offset: 0x00008380
		public bool RemoveTopic(string topic)
		{
			bool result = false;
			object obj = this.objLock;
			lock (obj)
			{
				result = this.Topics.Remove(topic);
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000284 RID: 644 RVA: 0x0000A1D4 File Offset: 0x000083D4
		public override string ToString()
		{
			return string.Format("WebSocketSession[{0}][{1}]", this.Remote, SoftBasic.GetTimeSpanDescription(DateTime.Now - this.OnlineTime));
		}

		// Token: 0x040000CF RID: 207
		private object objLock = new object();
	}
}
